function createjsDOMenu(customReports){
	var menuBar = new jsDOMenuBar("static", "staticMenuBar", "", "",330); //CREATE MAIN MENU ITEMS
	// Download format option.
	var downloadFormatMenu = new jsDOMenu(170);
	with(downloadFormatMenu){
		var zipMenuTitle = "Zipped PDF", pdfMenuTitle = "PDF";
		if(getCookie("downloadFormat") == "PDF"){
			pdfMenuTitle = "\u2714 " + pdfMenuTitle;
		}else{
			zipMenuTitle = "\u2714 " + zipMenuTitle;
		}
		addMenuItem(new menuItem(zipMenuTitle, "","code:setDownloadType('ZIP')"));
		addMenuItem(new menuItem(pdfMenuTitle, "","code:setDownloadType('PDF')"));
	}

	// Open Custom Reports menu
	var customReportsMenu = null;
	
	if(jsShowCustomReportsTab){
		customReportsMenu = new jsDOMenu(170);
		with(customReportsMenu){
			addMenuItem(new menuItem("Create Customised Report", "","code:openCustomReportsTab()"));
			addMenuItem(new menuItem("-"));
		}
		
		if(customReports != null){
			/*
			for(var i=0;i<customReports.length;i++){
				with (customReportsMenu) {
					addMenuItem(new menuItem(customReports[i].reportTitle, "item" + i));
				}

				settingsSubMenu = new jsDOMenu(150);
				with (settingsSubMenu) {
					addMenuItem(new menuItem("Rename", "", "code:renameCustomReport(" + customReports[i].reportTitlesId + ",'" + customReports[i].reportTitle + "')"));
					addMenuItem(new menuItem("Delete", "", "code:deleteCustomReport(" + customReports[i].reportTitlesId + ",'" + customReports[i].reportTitle + "')"));
					
				}

				customReportsMenu.items["item" + i].setSubMenu(settingsSubMenu); //ASSOCIATE SUB MENU WITH PARENT MENU ITEM
			}
			*/
			
			// if there are too many custom reports for this user, then group them into 
			// multiple sub-menus under the custom reports menu. Else, show them directly.
			if(customReports.length <= 10){
				createSimpleCustomReportsMenus(customReports, customReportsMenu);
			}else{
				createMultiLevelSimpleCustomReportsMenus(customReports, customReportsMenu);
			}
		}		
	}
	
	// Help Menu.
	var helpMenu = new jsDOMenu(170);
	with(helpMenu){
		addMenuItem(new menuItem("How to Add Companies", "","code:window.open('FAF_Help/FindaFirmHelp.html','helpwin')"));
	}


	with (menuBar) {
		addMenuBarItem(new menuBarItem(":: download format", downloadFormatMenu));
		// add this menu item only if the customer has the facility to create custom reports.
		if(jsShowCustomReportsTab){
			addMenuBarItem(new menuBarItem(":: custom reports", customReportsMenu));
		}
		addMenuBarItem(	new menuBarItem(":: help", helpMenu));
		addMenuBarItem(new menuBarItem(":: log out",null,"",true,"code:logOut(document.frm);"));
	}
}

function setDownloadType(typeNameToSet){
	setCookie("downloadFormat",  typeNameToSet, 1); // set the selected format for 1 day
	if(typeNameToSet == "PDF"){
		alert("The file will be about 50% larger than\n shown and will take longer to download");
	}
	document.getElementById("staticMenuBar").innerHTML = "";
	createjsDOMenu(null);
}

function openCustomReportsTab(){
	// open the custom reports tab on the mr_main.jsp page.
	// the custom report tab is the 4th tab in the "maintab" list (index 3).
	expandtab("maintab", 3);
}

function setCookie(c_name,value,exdays){
	var exdate=new Date();
	exdate.setDate(exdate.getDate() + exdays);
	var c_value=escape(value) + ((exdays==null) ? "" : "; expires="+exdate.toUTCString());
	document.cookie=c_name + "=" + c_value;
}

function getCookie(c_name){
	var i,x,y,ARRcookies=document.cookie.split(";");
	for (i=0;i<ARRcookies.length;i++){
		x=ARRcookies[i].substr(0,ARRcookies[i].indexOf("="));
		y=ARRcookies[i].substr(ARRcookies[i].indexOf("=")+1);
		x=x.replace(/^\s+|\s+$/g,"");
		if (x==c_name){
			return unescape(y);
		}
	}
}

//function used to create the custom reports menu when there are a limited number of custom reports.
function createSimpleCustomReportsMenus(customReports, customReportsMenu){
	for(var i=0;i<customReports.length;i++){
		with (customReportsMenu) {
			addMenuItem(new menuItem(customReports[i].reportTitle, "item" + i));
		}

		var settingsSubMenu = createRenameDeleteSubMenus(customReports[i]);
		
		customReportsMenu.items["item" + i].setSubMenu(settingsSubMenu); //ASSOCIATE SUB MENU WITH PARENT MENU ITEM
	}
}

// function to create multi-level menus, grouping reports into multiple sub-menus to prevent
// UI issues when the number of custom reports are large.
function createMultiLevelSimpleCustomReportsMenus(customReports, customReportsMenu){
	for(var i=0;i<customReports.length;i+=10){
		with (customReportsMenu) {
			addMenuItem(new menuItem("Reports " + (i + 1) + " to " + (i + 10), "Group" + i));
			
			var reportsMenu = new jsDOMenu(150);
			for(var j=i; j < (i+10);j++){
				if(j < customReports.length){
					with(reportsMenu){
						addMenuItem(new menuItem(customReports[j].reportTitle, "item" + j));
						var settingsSubMenu = createRenameDeleteSubMenus(customReports[j]);
						items["item" + j].setSubMenu(settingsSubMenu); 
					}
				}
			}
			items["Group" + i].setSubMenu(reportsMenu); 
		}		
	}
}

function createRenameDeleteSubMenus(customReport){
	var settingsSubMenu = new jsDOMenu(150);
	with (settingsSubMenu) {
		addMenuItem(new menuItem("Rename", "", "code:renameCustomReport(" + customReport.reportTitlesId + ",'" + customReport.reportTitle.replace("'", "\\'")  + "')"));
		addMenuItem(new menuItem("Delete", "", "code:deleteCustomReport(" + customReport.reportTitlesId + ",'" + customReport.reportTitle.replace("'", "\\'")  + "')"));
	}
	
	return settingsSubMenu;
}